﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Clancy.Data
{
    public class SurveilanceSetup
    {
        private List<SurveilanceObject> m_SurveilanceObjects = new List<SurveilanceObject>();

        [XmlElement]
        public SurveilanceObject[] SurveilanceObjects
        {
            get { return m_SurveilanceObjects.ToArray(); }
            set
            {
                m_SurveilanceObjects = new List<SurveilanceObject>();
                if (value != null)
                {
                    m_SurveilanceObjects.AddRange(value);
                }
            }
        }

        public static SurveilanceSetup FromXml(string xml)
        {
            XmlTextReader xmlReader = null;
            StringReader stringReader = null;
            try
            {
                XmlSerializer ser = new XmlSerializer(typeof(SurveilanceSetup));
                stringReader = new StringReader(xml);
                xmlReader = new XmlTextReader(stringReader);
                return ser.Deserialize(xmlReader) as SurveilanceSetup;
            }
            finally
            {
                if (xmlReader != null)
                {
                    xmlReader.Close();
                }
                if (stringReader != null)
                {
                    stringReader.Close();
                }
            }
        }

        public void Load(string filename)
        {
            SurveilanceSetup tempSetup = LoadFromXml(filename);
            if (tempSetup != null)
            {
                m_SurveilanceObjects.Clear();
                m_SurveilanceObjects.AddRange(tempSetup.SurveilanceObjects);
            }
        }

        public void Save(string filename)
        {
            SerializeToXml(this, filename);
        }

        public string ToXml()
        {
            XmlSerializer ser = new XmlSerializer(GetType());
            MemoryStream memStream = new MemoryStream();
            XmlTextWriter xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            ser.Serialize(xmlWriter, this);
            xmlWriter.Close();
            memStream.Close();
            string xml = Encoding.UTF8.GetString(memStream.GetBuffer());
            xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
            xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
            return xml.Trim() + "\0";
        }

        private static SurveilanceSetup LoadFromXml(string filename)
        {
            TextReader textReader = null;
            try
            {
                textReader = new StreamReader(filename);
                XmlSerializer deserializer = new XmlSerializer(typeof(SurveilanceSetup));

                return (SurveilanceSetup)deserializer.Deserialize(textReader);
            }
            finally
            {
                if (textReader != null)
                    textReader.Close();
            }
        }

        static private void SerializeToXml(SurveilanceSetup setup, string filename)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(SurveilanceSetup));
            TextWriter textWriter = new StreamWriter(filename);
            serializer.Serialize(textWriter, setup);
            textWriter.Close();
        }
    }
}